<?php
include_once (MODEL.'/Repository.php');
include_once (MODEL.'/ExchangeModel.php');
include_once (MODEL.'/ConversationModel.php');
include_once (MODEL.'/CommentModel.php');

class ExchangeRepository extends Repository
{
	public function GetOwnerByOfferID($offerID)
	{
		$offer = $this->DB->selectCollection('Offer')->find(array('_id' => new MongoId($offerID)));
		$cursor = $offer->getNext();
		$AccountID = $cursor['AccountID'];
		return $AccountID;
	}
	
	public function ProductInforms($offerID)
	{
		$Inform =  new ExchangeModel();
		$offer = $this->DB->selectCollection('Offer')->find(array('_id' => new MongoId($offerID)));
		$cursor = $offer->getNext();
		$Inform->Image = $cursor['Image'];
		$Inform->Description = $cursor['Description'];
		$Inform->Title = $cursor['Name'];
		$OwnerID = $cursor['AccountID'];
		$user = $this->DB->selectCollection('Account')->find(array('_id' => new MongoId($OwnerID)));
		$UserName = $user->getNext();
		$Inform->OwnerName = $UserName['FirstName']." ".$UserName['MiddleName']." ".$UserName['LastName'];
		$Inform->OwnerID = $OwnerID;
		$Inform->Avatar = $UserName['Avatar'];
		$Inform->hasPart = $cursor['hasPart'];
		$Inform->NumberOfPart = $cursor['NumberOfPart'];
		return $Inform;
	}
	
	public function GetConversation($offerID,$UserAccountID)
	{
		$cursor = $this->DB->selectCollection('Conversation')->find(array('OfferWantID' => $offerID));
		$listConvers = array();
		
		while ($cursor->hasNext()) {
			$conver = new ConversationModel();
			$result = $cursor->getNext();
			$converID =  $result['_id'];		
			$conver->ID = $converID;
			$conver->DateCreated = $result['DateCreated'];
			$conver->FirstContent = $result['firstContent'];
			
			$usrAcc =$result['UserAccountFirst'];
			$conver->UserAccountFirst = $usrAcc;
			
			if ($usrAcc==$UserAccountID) {
				$conver->IsCommenter = true;
			}
			$conver->checked = $result['Checked'];
			$conver->FirstAvatar = $result['UserAccountFirst'].".jpg";
			
			$user = $this->DB->selectCollection('Account')->find(array('_id' => new MongoId($usrAcc)));
			$UserName = $user->getNext();		
			$conver->UserName = $UserName['FirstName']." ".$UserName['MiddleName']." ".$UserName['LastName'];
			$conver->listComments = $this->GetComment($converID."");						
			$listConvers[] =$conver; 			
		}
		return $listConvers;
	}
	
	public function GetComment($converID)
	{
		$cursor = $this->DB->selectCollection('Comment')->find(array('ConversationID' => $converID));
		$listComments = array();
		
		while ($cursor->hasNext()) {
			$comment = new CommentModel();
			$result = $cursor->getNext();
			
			$AccountID = $result['AccountID'];
			$comment->AccountID = $AccountID;
			$comment->Avatar = $result['AccountID'].".jpg";
			$comment->Content = $result['Content'];
			$comment->CreatedDate = $result['CreateDated'];
			$user = $this->DB->selectCollection('Account')->find(array('_id' => new MongoId($AccountID)));
			$UserName = $user->getNext();
			$comment->CommenterName = $UserName['FirstName']." ".$UserName['MiddleName']." ".$UserName['LastName'];
			$listComments[] =$comment;
		}
		
		return $listComments;
	}
	
	public function PutComment($content,$convID,$AccountID,$offerID)
	{
		$this->DB->selectCollection('Comment')->insert(array('AccountID'=>$AccountID,'Content'=>$content,'ConversationID'=>$convID,'CreateDated'=>new MongoDate()));
		$cursor = $this->DB->selectCollection('Exchange')->findOne(array('OfferID' => $offerID));
		$numOfComment = ($cursor['NumOfComment'])+1;		
		$tem = $this->DB->selectCollection('Exchange')->update(array('OfferID'=>$offerID),array('$set'=>array('NumOfComment'=>new MongoInt32($numOfComment))),array("upsert"=>false));
	}
	
	public function PutConver($firstContent,$AccountID,$OfferID)
	{
		
		$numOfConver = $this->DB->selectCollection('Conversation')->find(array('OfferWantID'=>$OfferID))->count();
		if($numOfConver==null){
			$this->DB->selectCollection('Exchange')->insert(array('OfferID'=>$OfferID,'NumOfComment'=>new MongoInt32(1),'Matched'=>false));
		}
		else{
			$cursor = $this->DB->selectCollection('Exchange')->findOne(array('OfferID' => $OfferID));
			$numOfComment = $cursor['NumOfComment']+1;
			$this->DB->selectCollection('Exchange')->update(array('OfferID'=>$OfferID),array('$set'=>array('NumOfComment'=>new MongoInt32($numOfComment))),array("upsert"=>false));
		}
		$this->DB->selectCollection('Conversation')->insert(array('UserAccountFirst'=>$AccountID,'Checked'=>false,'firstContent'=>$firstContent,'OfferWantID'=>$OfferID,'DateCreated'=>new MongoDate()));
	}
	public function PutCheck($conversaID,$numPart,$offerID)
	{
		$temp = $this->DB->selectCollection('Conversation')->update(array('_id'=>new MongoId($conversaID)),array('$set'=>array('Checked'=>true)),array("upsert"=>false));
		$template = $this->DB->selectCollection('Offer')->update(array('_id'=>new MongoId($offerID)),array('$set'=>array('hasPart'=>new MongoInt32($numPart))),array("upsert"=>false));
	}
	
	public function MakeMatch($offerID)
	{
		$this->DB->selectCollection('Exchange')->update(array('OfferID'=>$OfferID),array('$set'=>array('Matched'=>true)),array("upsert"=>false));
	}
	
	public function GetNumComment($OfferID)
	{
		$cursor = $this->DB->selectCollection('Exchange')->findOne(array('OfferID' => $OfferID));
		$numOfComment = $cursor['NumOfComment'];
		return $numOfComment;
	}
	
	public function GetListExchange($limit)
	{
		$cursor = $this->DB->selectCollection('Exchange')->find()->limit($limit);
		$listExchange = array();
		
		while($cursor->hasNext())
		{
			$exchange = new ExchangeModel();
			$result = $cursor->getNext();
			$offerId = $result['OfferID'];
			$Offer = $this->DB->selectCollection('Offer')->findOne(array('_id'=>new MongoId($offerId)));
			$exchange->offerID = $offerId;
			$exchange->Title=$Offer['Name'];
			$exchange->Image = $Offer['Image'];
			$AccountID= $Offer['AccountID'];
			$exchange->Location = $Offer['Location'];
			$exchange->OwnerID =$AccountID;
			$user = $this->DB->selectCollection('Account')->findOne(array('_id'=>$AccountID));
			$exchange->OwnerName = $user['FirstName']." ".$user['MiddleName']." ".$user['LastName'];
			$exchange->Description = $Offer['Description'];
			$listExchange[] = $exchange;
			
		}
		return $listExchange;
	}
	
	
}